package com.springsoft.myjavalib;

import java.util.ArrayList;
import java.util.List;

public class myTest {
    public static void main(String[] args) {
//        // 监听指定的端口
//        int port = 12022;
//        ServerSocket server = null;
//        try {
//            server = new ServerSocket(port);
//
//
//            // server将一直等待连接的到来
//            System.out.println("server将一直等待连接的到来");
//            Socket socket = server.accept();
//            // 建立好连接后，从socket中获取输入流，并建立缓冲区进行读取
//            InputStream inputStream = socket.getInputStream();
//            byte[] bytes = new byte[1024];
//            int len;
//            StringBuilder sb = new StringBuilder();
//            //只有当客户端关闭它的输出流的时候，服务端才能取得结尾的-1
//            while ((len = inputStream.read(bytes)) != -1) {
//                // 注意指定编码格式，发送方和接收方一定要统一，建议使用UTF-8
//                sb.append(new String(bytes, 0, len, "UTF-8"));
//            }
//            System.out.println("get message from client: " + sb);
//
//            OutputStream outputStream = socket.getOutputStream();
//            outputStream.write("Hello Client,I get the message.".getBytes("UTF-8"));
//
//            inputStream.close();
//            outputStream.close();
//            socket.close();
//            server.close();
//        } catch (IOException e) {
//            e.printStackTrace();
//        }

        //阶乘 3! = 3*2*1
        System.out.println("3的阶乘=" + jiecheng(3));

        //杨辉三角形
        System.out.println("杨辉三角形----start----");
        int row = 10;
        for (int i = 1; i <= row; i++) {
            for (int j = 1; j <= row - i; j++) {
                System.out.print("  ");
            }
            for (int j = 1; j <= i; j++) {
                System.out.printf("%4d", yanghui(i, j));
            }
            System.out.println();
        }
        System.out.println("杨辉三角形----end----");

        System.out.println("九九乘法表---start---");
        chengfakoujue(9);
        System.out.println("九九乘法表---end---");

        String hStr = "abcdcba";
        System.out.println(hStr + "是回文字符串吗？" + (huiwen(hStr) ? "是" : "不是"));

        System.out.println("冒泡排序----");
        int[] maopao = maopao(new int[]{1,81,91,15,31,71,11,1,1,12});
        for (Integer b : maopao) {
            System.out.println("冒泡升序：" + b);
        }
    }

    //阶乘
    public static int jiecheng(int num) {
        if (num == 1) {
            return 1;
        } else {
            return num * jiecheng(num - 1);
        }
    }

    //杨辉三角形
    public static int yanghui(int i, int j) {
        if (j == 1 || i == j) {
            return 1;
        } else {
            return yanghui(i - 1, j - 1) + yanghui(i - 1, j);
        }
    }

    //九九乘法表
    public static void chengfakoujue(int num) {
        if (num == 1) {
            System.out.println("1*1=1");
        } else {
            chengfakoujue(num - 1);
            for (int i = 1; i <= num; i++) {
                System.out.print(i + "*" + num + "=" + i * num + " ");
            }
            System.out.println();
        }
    }

    //回文字符串判断
    public static boolean huiwen(String str) {
        int startP = 0;
        int endP = str.length() - 1;
        if (endP > startP) {
            if (str.charAt(startP) != str.charAt(endP)) {
                return false;
            } else {
                return huiwen(str.substring(startP + 1).substring(0, endP - 1));
            }
        }
        return true;
    }

    //冒泡排序，升序
    public static int[] maopao(int s[]) {
        int temp = 0;
        for (int i = 0; i < s.length; i++) {
            for (int j = i + 1; j < s.length; j++) {
                if (s[i] > s[j]) {
                    temp = s[i];
                    s[i] = s[j];
                    s[j] = temp;
                }
            }
        }
        return s;
    }
}
